home *** CD-ROM | disk | FTP | other *** search
/ 5 Star Games: DOS Edition 2 / 5 Star Games - DOS Edition (1995)(Ready to Run).iso / dbc / db_date.c < prev    next >
C/C++ Source or Header  |  1992-02-28  |  13KB  |  516 lines

  1. /****************************************************************************/
  2. /*                       DATABOSS MODULE: DB_DATE.C                         */
  3. /****************************************************************************/
  4.  
  5. #include "db_lsc.h"
  6.  
  7. #ifdef __TURBOC__
  8.     #include <conio.h>
  9. #else
  10.     #include <graph.h>
  11. #endif
  12. #include <dos.h>
  13. #include <math.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "db_types.h"
  18. #include "db_conio.h"
  19. #include "db_key.h"
  20. #include "db_math.h"
  21. #include "db_sets.h"
  22. #include "db_str.h"
  23. #include "db_date.h"
  24. #include "db_file.h"
  25. #include "db_funcs.h"
  26.  
  27. /****************************  GLOBAL CONSTANTS  ****************************/
  28.  
  29. const byte Limit[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31};
  30.  
  31. /****************************  GLOBAL VARIABLES  ****************************/
  32.  
  33. set       _NUMERIC;
  34. _datetyp  dateformat;
  35. uchar     defaultyr[3];
  36. byte      daypos;
  37. byte      mthpos;
  38. byte      yrpos;
  39.  
  40. /***************************  INTERNAL VARIABLES  ***************************/
  41.  
  42. static bool initialized = False;
  43.  
  44. /*****************************  IMPLEMENTATION  *****************************/
  45.  
  46. bool leapyr(int y)
  47. {
  48.     return ((bool) (((y % 4 == 0) && (y % 100 > 0)) || (y % 400 == 0)));
  49. }
  50.  
  51. strptr slash(_timestr sout, _timestr datein)
  52. {
  53.     uchar temp1[3],temp2[3],temp3[5];
  54.  
  55.     if (yrpos == 5) {
  56.         strcopy(temp1,datein,0,2);
  57.         strcopy(temp2,datein,2,2);
  58.         strcopy(temp3,datein,4,4);
  59.         strconcat(sout,temp1,"/",temp2,"/",temp3,NULL);
  60.     }
  61.     else
  62.         strconcat(sout,strcopy(temp1,datein,0,4),"/",
  63.                             strcopy(temp2,datein,4,2),"/",
  64.                             strcopy(temp3,datein,6,4),NULL);
  65.     return (sout);
  66. }
  67.  
  68. strptr slasht(_timestr sout, _timestr timein)
  69. {
  70.     uchar dpt[2];
  71.     uchar temp1[3],temp2[3],temp3[5];
  72.  
  73.     strcpy(dpt, ".");
  74.     dpt[0] = *_dcpt;
  75.     strconcat(sout,strcopy(temp1,timein,0,2),timesep,
  76.                         strcopy(temp2,timein,2,2),timesep,
  77.                         strcopy(temp3,timein,4,2),NULL);
  78.     if (strlen(timein) > 6) strconcat(sout,sout,dpt,strcopy(temp1,timein,6,2),NULL);
  79.     return (sout);
  80. }
  81.  
  82. bool chkdate(_datestr date)
  83. {
  84.     int      i;
  85.     strptr   sourceDate,destDate;
  86.     _datestr tempDate;
  87.     uchar    temp[5];
  88.     bool     date_ok,leapyear;
  89.     int      year,month,day;
  90.  
  91. /* SN 3.5
  92.     for (s=date, d=tdate, i=1; (*s != '\0') && (i <= 8); s++, i++) *(d++) = (uchar) ((*s == ' ') ? '0' : *s);
  93. */
  94.  
  95.    sourceDate = date;                                                    /* SN 3.5 */
  96.    destDate   = tempDate;                                                /* SN 3.5 */
  97.    strcpy(destDate, sourceDate);                                         /* SN 3.5 */
  98.    if (*(destDate + 6) == ' ') *(destDate + 6) = '\0';                   /* SN 3.5 */
  99.    else {                                                                /* SN 3.5 */
  100.       if (*(destDate + 7) == ' ') *(destDate + 7) = '\0';                /* SN 3.5 */
  101.    }                                                                     /* SN 3.5 */
  102.     if (strlen(destDate) <= 6) strinsert(defaultyr, destDate, yrpos-1);   /* SN 3.5 */
  103.  
  104.     date_ok  = False;
  105.     day      = ival(strcopy(temp,destDate,daypos-1,2));
  106.     month    = ival(strcopy(temp,destDate,mthpos-1,2));
  107.     year     = ival(strcopy(temp,destDate,yrpos-1,4));
  108.     leapyear = leapyr(year);
  109.  
  110.     if (((month > 0)  && (month < 13)) && ((day > 0)   && (day < 32)))                                 date_ok = True;     /* SN 3.5 */
  111.     if (((month == 4) || (month == 6)  || (month == 9) || (month == 11)) && (day > 30))                date_ok = False;    /* SN 3.5 */
  112.     if ((date_ok      && (month == 2)) && ((leapyear   && (day > 29))   || (!leapyear && (day > 28)))) date_ok = False;    /* SN 3.5 */
  113.     return (date_ok);
  114. }
  115.  
  116. long ctod(_datestr date)
  117. {
  118.     int      i;
  119.     strptr   sourceDate,destDate;
  120.     _datestr tempDate;
  121.     uchar    temp[5];
  122.     int      year,month,day;
  123.     long     julian;
  124.  
  125. /*  SN 3.5
  126.     for (s=date, d=tdate, i=1; (*s != '\0') && (i <= 8); s++, i++) *(d++) = (uchar) ((*s == ' ') ? '0' : *s);
  127.     *d = '\0';
  128. */
  129.  
  130.    sourceDate = date;                                                    /* SN 3.5 */
  131.    destDate   = tempDate;                                                /* SN 3.5 */
  132.    strcpy(destDate, sourceDate);                                         /* SN 3.5 */
  133.    if (*(destDate + 6) == ' ') *(destDate + 6) = '\0';                   /* SN 3.5 */
  134.    else {                                                                /* SN 3.5 */
  135.       if (*(destDate + 7) == ' ') *(destDate + 7) = '\0';                /* SN 3.5 */
  136.    }                                                                     /* SN 3.5 */
  137.     if (strlen(destDate) <= 6) strinsert(defaultyr, destDate, yrpos-1);   /* SN 3.5 */
  138.  
  139.     day = ival(strcopy(temp,destDate,daypos - 1,2));
  140.     if (day == 0) day = 1;
  141.     month  = ival(strcopy(temp,destDate,mthpos-1,2));
  142.     year   = ival(strcopy(temp,destDate,yrpos-1,4));
  143.     julian = (long) fint(fint(30.57 * month) + fint(365.25 * year - 395.25) + day);
  144.     if (month > 2) {
  145.         julian--;
  146.         if (!leapyr(year)) julian--;
  147.     }
  148.     return (julian);
  149. }
  150.  
  151. void dton(long jul, int *day, int *month, int *year)
  152. {
  153.     double temp,julian;
  154.     int    leapday;
  155.  
  156.     julian  = (double) jul;
  157.     *year   = (int) (fint(julian/365.26) + 1);
  158.     temp    = fint(julian+fint(395.25-(365.25*(*year))));
  159.     leapday = 1;
  160.  
  161.     if (!leapyr(*year)) leapday = 2;
  162.     if (temp > (double) (91 - leapday)) temp += (double) leapday;
  163.     *month = (int) fint(temp/30.57);
  164.     *day   = (int) fint(temp - fint(30.57 * (*month)));
  165.     if (*month > 12) {
  166.         *month = 1;
  167.         (*year)++;
  168.     }
  169. }
  170.  
  171. strptr dtoc(_datestr date, long julian)
  172. {
  173.     uchar  sday[7],smonth[7],syear[7];
  174.     int    day,month,year;
  175.     strptr dptr;
  176.  
  177.     dton(julian,&day,&month,&year);
  178.     istr(sday,day, 2);
  179.     istr(smonth,month, 2);
  180.     istr(syear,year, 4);
  181.    memmove(&date[daypos-1],sday,2);
  182.     memmove(&date[mthpos-1],smonth,2);
  183.     memmove(&date[yrpos-1],syear,4);
  184.     date[8] = '\0';
  185.     for (dptr = date; *dptr != '\0'; dptr++) if (*dptr == ' ') *dptr = '0';
  186.     return (date);
  187. }
  188.  
  189. int dow(long jul)
  190. {
  191.     double julian;
  192.  
  193.     julian = (double) (jul-1);
  194.     return ((int) fint(julian - (fint(julian / 7) * 7) + 1));
  195. }
  196.  
  197. strptr cdow(_str9 sout, long julian)
  198. {
  199.     int d;
  200.  
  201.     d = dow(julian);
  202.     return (strcpy(sout, DayTab[((d >= 1) && (d <= 7)) ? d : 0]));
  203. }
  204.  
  205. strptr cmonth(_str9 sout, long julian)
  206. {
  207.     int day,mon,year;
  208.  
  209.     dton(julian,&day,&mon,&year);
  210.     return (strcpy(sout,MthTab[((mon >= 0) && (mon <= 12)) ? mon : 0]));
  211. }
  212.  
  213. int getday(long julian)
  214. {
  215.     int day,month,year;
  216.  
  217.     dton(julian,&day,&month,&year);
  218.     return (day);
  219. }
  220.  
  221. int getmonth(long julian)
  222. {
  223.     int day,month,year;
  224.  
  225.     dton(julian,&day,&month,&year);
  226.     return (month);
  227. }
  228.  
  229. int getyear(long julian)
  230. {
  231.     int day,month,year;
  232.  
  233.     dton(julian,&day,&month,&year);
  234.     return (year);
  235. }
  236.  
  237. strptr idxdate(_datestr sout, _datestr dat)
  238. {
  239.     double r;
  240.  
  241.     r = (double) ctod(dat);
  242.     fstr(sout, r, 6, 0);
  243.    pad(sout, strip(sout, sout), 8, Right);
  244.     return (sout);
  245. }
  246.  
  247. strptr decdate(_datestr sout, _datestr dat)
  248. {
  249.     double r;
  250.  
  251.     r = 99999999.0 - ctod(dat);
  252.    fstr(sout, r, 8, 0);
  253.     return (sout);
  254. }
  255.  
  256. void datediff(long jul1, long jul2, int *d, int *m, int *y)
  257. {
  258.     int d1,d2,m1,m2,y1,y2,sav_m2,back_month;
  259.     long swapthem;
  260.  
  261.     if (jul1 > jul2) {
  262.         swapthem = jul1;
  263.         jul1 = jul2;
  264.         jul2 = swapthem;
  265.     }
  266.  
  267.     dton(jul1,&d1,&m1,&y1);
  268.     dton(jul2,&d2,&m2,&y2);
  269.     sav_m2 = m2;
  270.     back_month = m2-1;
  271.     if (back_month == 0) back_month = 12;
  272.     *y = y2 - y1;
  273.     if ((m2 < m1) || ((m2 == m1) && (d2 < d1))) {
  274.         (*y)--;
  275.         if (m2 < m1) m2 += (d2 < d1) ? 11 : 12;
  276.         if (d2 < d1) {
  277.             if (sav_m2 == m1) m2 += 11;
  278.             d2 = d2 + Limit[back_month-1];
  279.             if (back_month == 2) d2--;
  280.         }
  281.     }
  282.     *m = m2 - m1;
  283.     if ((sav_m2 > m1) && (d2 < d1)) {
  284.         (*m)--;
  285.         d2 += Limit[back_month-1];
  286.         if (back_month == 2) d2--;
  287.     }
  288.     *d = d2 - d1;
  289. }
  290.  
  291. int leapdays(long jul1, long jul2)
  292. {
  293.     int   i,leaps,d1,d2,m1,m2,y1,y2;
  294.     long swapthem;
  295.  
  296.     leaps = 0;
  297.     if (jul1 > jul2) {
  298.         swapthem = jul1;
  299.         jul1 = jul2;
  300.         jul2 = swapthem;
  301.     }
  302.     dton(jul1,&d1,&m1,&y1);
  303.     dton(jul2,&d2,&m2,&y2);
  304.     for (i = y1; i <= y2; i++) if (leapyr(i)) leaps++;
  305.     if (leapyr(y1) && (m1 > 2)) leaps--;
  306.     if (leapyr(y2) && (m2 == 1) || ((m2 == 2) && (d2 <= 29))) leaps--;
  307.     return (leaps);
  308. }
  309.  
  310. strptr datum(_str30 sout, _datestr sdate)
  311. {
  312.     int   i;
  313.     long d;
  314.     uchar s1[40],s2[40];
  315.  
  316.     d = ctod(sdate);
  317.     cdow(s1,d);
  318.     i = strlen(s1);
  319.     while (i && (s1[i-1] == ' ')) i--;
  320.     s1[i] = '\0';
  321.     strconcat(s1,s1,", ",cmonth(s2,d),NULL);
  322.     i = strlen(s1);
  323.     while (i && (s1[i-1] == ' ')) i--;
  324.     s1[i] = '\0';
  325.     istr(s2, getday(d), 2);
  326.     strconcat(s1,s1," ",s2,", ",NULL);
  327.     istr(s2,getyear(d), 4);
  328.     strconcat(sout,s1,s2,".",NULL);
  329.     i = strlen(sout);
  330.     while (i < 30) sout[i++] = ' ';
  331.     sout[i] = '\0';
  332.     return (sout);
  333. }
  334.  
  335. strptr days(_str6 sout, long d1, long d2)
  336. {
  337.     int d,m,y;
  338.  
  339.     datediff(d1,d2,&d,&m,&y);
  340.    istr(sout, d, 2);
  341.     return (sout);
  342. }
  343.  
  344. strptr months(_str6 sout, long d1, long d2)
  345. {
  346.     int d,m,y;
  347.  
  348.     datediff(d1,d2,&d,&m,&y);
  349.     istr(sout,m, 2);
  350.     return (sout);
  351. }
  352.  
  353. strptr years(_str6 sout, long d1, long d2)
  354. {
  355.     int d,m,y;
  356.  
  357.     datediff(d1,d2,&d,&m,&y);
  358.     istr(sout,y, 4);
  359.     return (sout);
  360. }
  361.  
  362. strptr sysdate(_datestr sout)
  363. {
  364.     union REGS regs;
  365.     uchar d[7],m[7],y[7];
  366.     strptr sptr;
  367.  
  368.     regs.x.ax = 0x2A00;
  369.     intdos(®s,®s);
  370.     istr(d,regs.h.dl, 2);
  371.     istr(m,regs.h.dh, 2);
  372.     istr(y,regs.x.cx, 4);
  373.     memmove(&sout[daypos-1],d,2);
  374.     memmove(&sout[mthpos-1],m,2);
  375.     memmove(&sout[yrpos-1],y,4);
  376.     sout[8] = '\0';
  377.     for (sptr = sout; *sptr ; sptr++) if (*sptr == ' ') *sptr = '0';    /* SN 3.5 */
  378.     return (sout);
  379. }
  380.  
  381. strptr systime(_timestr sout)
  382. {
  383.     union REGS regs;
  384.     uchar h[7],m[7],s[7],d[7];
  385.  
  386.     regs.x.ax = 0x2C00;
  387.     intdos(®s,®s);
  388.     istr(h,regs.h.ch,2);
  389.     istr(m,regs.h.cl,2);
  390.     istr(s,regs.h.dh,2);
  391.     istr(d,regs.h.dl,2);
  392.     if (*h == ' ') *h = '0';
  393.     if (*m == ' ') *m = '0';
  394.     if (*s == ' ') *s = '0';
  395.     if (*d == ' ') *d = '0';
  396.     return (strconcat(sout,h,m,s,d,NULL));
  397. }
  398.  
  399. double realtime(void)
  400. {
  401.     union REGS regs;
  402.  
  403.     regs.x.ax = 0x2C00;
  404.     intdos(®s,®s);
  405.     return ((3600.0 * regs.h.ch) +
  406.        (60.0 * regs.h.cl)   +
  407.        (1.0 * regs.h.dh)    +
  408.        ((1.0 * regs.h.dl)/100.0));
  409. }
  410.  
  411. strptr get_time(_timestr sout, _timemode mode)
  412. {
  413.     _timestr outtime,temp;
  414.     uchar hour_str[7];
  415.     int hour;
  416.  
  417.     slasht(outtime,strcopy(outtime,systime(outtime),0,6));
  418.     hour = ival(strcopy(temp,outtime,0,2));
  419.     istr(hour_str,hour, 2);
  420.     strconcat(outtime,hour_str,strcopy(temp,outtime,2,6),NULL);
  421.     if (mode == _AmPm) strcopy(outtime,outtime,0,5);
  422.     if (mode != _24Hr) {
  423.         if (hour < 12)
  424.             strconcat(outtime,outtime,_AM_,NULL);
  425.         else {
  426.             strconcat(outtime,outtime,_PM_,NULL);
  427.             if (hour > 12) {
  428.                 strdelete(outtime,0,2);
  429.                 istr(hour_str,hour-12, 2);
  430.                 strconcat(outtime,hour_str,outtime,NULL);
  431.             }
  432.         }
  433.     }
  434.     return ((mode==_Full) ? strcpy(sout,outtime) : strcopy(sout,outtime,0,8));
  435. }
  436.  
  437. strptr strtime(_timestr sout, byte mode, _timestr timein)
  438. {
  439.     int tr;
  440.     uchar dpt[2];
  441.     uchar hrss[3], mins[3], secs[3], huns[3], ampms[4];
  442.  
  443.     strcpy(dpt, ".");
  444.     dpt[0] = *_dcpt;
  445.     strcopy(hrss,timein,0,2);
  446.     strcopy(mins,timein,2,2);
  447.     strcopy(secs,timein,4,2);
  448.     strcopy(huns,timein,6,2);
  449.     sout[0] = '\0';
  450.     tr = ival(hrss);
  451.     if (tr >= 12) strcpy(ampms,_PM_); else strcpy(ampms,_AM_);
  452.     if (((mode & _12hr)) && (tr > 12)) tr = (tr % 13) + 1;            /* SN 3.5 */
  453.     istr(hrss,tr, 2);
  454.     if ((mode & _hours)) strcpy(sout,hrss);                           /* SN 3.5 */
  455.     if ((mode & _mins))  strconcat(sout,sout,timesep,mins,NULL);      /* SN 3.5 */
  456.     if ((mode & _secs))  strconcat(sout,sout,timesep,secs,NULL);      /* SN 3.5 */
  457.     if ((mode & _huns))  strconcat(sout,sout,dpt,huns,NULL);          /* SN 3.5 */
  458.     if ((mode & _am_pm)) strcat(sout,ampms);                          /* SN 3.5 */
  459.     return(sout);
  460. }
  461.  
  462. void show_time(int x, int y, int attr, _timemode mode)
  463. {
  464.     _timestr outtime;
  465.     byte tb,saveattr;
  466.  
  467.     getscreeninfo(&tb,&tb,&tb,&saveattr);
  468.     textattr(attr);
  469.     while (!kpressed()) {
  470.         get_time(outtime,mode);
  471.         gotoxy(x,y);
  472.         cwrite(outtime);
  473.     }
  474.     textattr(saveattr);
  475. }
  476.  
  477. bool long_date(string field)
  478. {
  479.     return ((bool) (chkdate(field) &&
  480.                        (strchr(field,' ') == NULL) &&
  481.                        (strlen(field) == 8)));
  482. }
  483.  
  484. /**********************  UNIT INITIALIZATION/EXIT CODE  *********************/
  485.  
  486. void db_date_init(void)
  487. {
  488.     _datestr td;
  489.    string   numericCharacters;
  490.  
  491.     if (!initialized) {
  492.         initialized = True;
  493.         db_key_init();
  494.         dateformat = cdi.date_format;
  495.         switch (dateformat) {
  496.             case USA: daypos = 3; mthpos = 1; yrpos = 5; break;
  497.             case EUR: daypos = 1; mthpos = 3; yrpos = 5; break;
  498.             case JAP: daypos = 7; mthpos = 5; yrpos = 1; break;
  499.         }
  500.         strcopy(defaultyr,sysdate(td),yrpos-1,2);
  501.         *_dcpt         = *cdi.dec_sep;
  502.         *_thou         = *cdi.thou_sep;
  503.         *datesep       = *cdi.date_sep;
  504.         *timesep       = *cdi.time_sep;
  505.       *(_dcpt + 1)   = '\0';
  506.       *(_thou + 1)   = '\0';
  507.       *(datesep + 1) = '\0';
  508.       *(timesep + 1) = '\0';
  509.       strcpy(numericCharacters, "0123456789- ");
  510.       strcat(numericCharacters, _dcpt);
  511.         set_strmake(_NUMERIC,numericCharacters);
  512.     }
  513. }
  514.  
  515. /*****************************  END OF DB_DATE.C ****************************/
  516.